package com.platform.cdcs.fragment.choose;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.platform.cdcs.R;
import com.platform.cdcs.adapter.ProductItemAdatper;
import com.platform.cdcs.model.BaseObjResponse;
import com.platform.cdcs.model.ProductList;
import com.platform.cdcs.model.RefershEvent;
import com.platform.cdcs.tool.Constant;
import com.sherchen.slidetoggleheader.views.ObservableXListView;
import com.sherchen.slidetoggleheader.views.XListView;
import com.trueway.app.uilib.adapter.EnhancedAdapter;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.tool.Utils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;

/**
 * Created by holytang on 2017/10/28.
 */
public class PHFragment extends BaseFragment {

    private ObservableXListView listView;
    private String mClass;
    private ItemAdapter adatper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mClass = getArguments().getString("class");
        }
        adatper = new ItemAdapter(getContext());
    }

    @Override
    public void initView(View view) {
        setTitle("选择批号");
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        initLoadImg(view.findViewById(R.id.load));
        hideThisToolBar(view);
        listView = (ObservableXListView) view.findViewById(android.R.id.list);
//        listView.setXListViewListener(new XListView.IXListViewListener() {
//            @Override
//            public void onRefresh() {
////                adapter.clear();
////                adapter.notifyDataSetChanged();
////                requestData(false);
//            }
//
//            @Override
//            public void onLoadMore() {
//            }
//        });
        listView.setAdapter(adatper);
        listView.setPullRefreshEnable(false);
//        listView.setAdapter(adapter);
        requestData(true);
    }

    @Override
    public int layoutId() {
        return R.layout.listview;
    }

    private void requestData(boolean show) {
        if (show) {
            showLoadImg();
        }
        Map<String, String> map = new HashMap<>();
        map.put("itemCode", getArguments().getString("code"));
        map.put("whName", getArguments().getString("pos", ""));
        getHttpClient().post().url(Constant.PRODUCT_LOTS_LIST).params(Constant.makeParam(map)).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getContext(), R.string.server_error);
            }

            @Override
            public void onResponse(String s, int i) {
                dismissLoadImg();
                Type mType = new TypeToken<BaseObjResponse<ProductList>>() {
                }.getType();
                BaseObjResponse<ProductList> response = new Gson().fromJson(s, mType);
                if ("1".equals(response.getResult().getCode())) {
                    adatper.addAll(response.getResult().getProductList());
                    adatper.notifyDataSetChanged();
                } else {
                    Utils.showToast(getContext(), response.getResult().getMsg());
                }
            }
        });
    }

    private class ItemAdapter extends EnhancedAdapter<ProductList.ProductItem> {

        private View.OnClickListener itemListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProductList.ProductItem item = (ProductList.ProductItem) view.getTag(R.id.button1);
                RefershEvent event = new RefershEvent();
                try {
                    event.mclass = Class.forName(mClass);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                event.oclass = PHFragment.class;
                event.bundle = new Bundle();
                event.bundle.putSerializable("model", item);
                EventBus.getDefault().post(event);
                getActivity().finish();
            }
        };

        public ItemAdapter(Context context) {
            super(context);
        }

        @Override
        protected void bindView(View paramView, Context paramContext, int position) {
            ViewHolder holder = (ViewHolder) paramView.getTag();
            ProductList.ProductItem item = getItem(position);
            if (TextUtils.isEmpty(item.getExpDate())) {
                holder.textView.setVisibility(View.GONE);
            } else {
                holder.textView.setVisibility(View.VISIBLE);
                holder.textView.setText("失效期 " + item.getExpDate());
            }
            if (!TextUtils.isEmpty(item.getLots())) {
                holder.titleView.setText(item.getLots());
            } else {
                holder.titleView.setText("序列号、批号未知产品");
            }
            holder.descView.setText(String.format("%s %s", item.getQty(), item.getUom()));
            paramView.setTag(R.id.button1, item);
        }

        @Override
        protected View newView(Context context, int position, ViewGroup viewgroup) {
            View rootView = inflater.inflate(R.layout.three_item, viewgroup, false);
            ViewHolder holder = new ViewHolder();
            holder.titleView = (TextView) rootView.findViewById(R.id.title);
            holder.descView = (TextView) rootView.findViewById(R.id.desc);
            holder.descView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            holder.textView = (TextView) rootView.findViewById(R.id.text);
            rootView.setOnClickListener(itemListener);
            rootView.setTag(holder);
            return rootView;
        }

    }

    private class ViewHolder {
        TextView titleView, descView, textView;
    }
}
